package com.dhecp.project.system.controller;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.enums.VersionFuncType;
import com.dhecp.core.enums.VersionOperateType;
import com.dhecp.project.system.domain.SysVersionItem;
import com.dhecp.project.system.service.SysVersionItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysVersionItemController
 * @Description TODO
 * @Author alula
 * @Date 2020-03-12 15:22
 */
@Controller
@RequestMapping("/system/versionitem")
public class SysVersionItemController extends BaseController {

    private String prefix = "system/version";

    @Autowired
    private SysVersionItemService sysVersionItemService;

    @RequiresPermissions("system:version:view")
    @GetMapping()
    public String versionitem(@RequestParam("vid") String vid, ModelMap mmap)
    {
        VersionFuncType[] versionFuncTypes = VersionFuncType.values();
        VersionOperateType[] versionOperateTypes = VersionOperateType.values();
        mmap.put("versionId", vid);
        mmap.put("versionFuncTypes", versionFuncTypes);
        mmap.put("versionOperateTypes", versionOperateTypes);
        return prefix + "/versionitem";
    }

    @RequiresPermissions("system:version:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params)
    {
        startPage();
        List<SysVersionItem> list = sysVersionItemService.queryVersionItemList(params);
        return getDataTable(list);
    }

    @RequiresPermissions("system:version:view")
    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("versioinItem", sysVersionItemService.queryVersionItemById(id));
        return prefix + "/versionitem_detail";
    }

    @RequiresPermissions("system:version:add")
    @RequestMapping("/add")
    public String add(@RequestParam("vid") String vid, ModelMap mmap) {
        SysVersionItem versioinItem = new SysVersionItem();
        versioinItem.setVersionId(vid);
        versioinItem.setDeleted(false);
        versioinItem.setFuncType(VersionFuncType.table);
        versioinItem.setOperateType(VersionOperateType.insert);
        VersionFuncType[] versionFuncTypes = VersionFuncType.values();
        VersionOperateType[] versionOperateTypes = VersionOperateType.values();
        mmap.put("versionFuncTypes", versionFuncTypes);
        mmap.put("versionOperateTypes", versionOperateTypes);
        mmap.put("versioinItem", versioinItem);
        return prefix + "/versionitem_add";
    }

    @Log(title = "软件版本明细新增", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:version:add")
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(SysVersionItem sysVersionItem)
    {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= sysVersionItemService.insertVersionItem(sysVersionItem);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


    @RequiresPermissions("system:version:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        SysVersionItem versioinItem = sysVersionItemService.queryVersionItemById(id);
        VersionFuncType[] versionFuncTypes = VersionFuncType.values();
        VersionOperateType[] versionOperateTypes = VersionOperateType.values();
        mmap.put("versionFuncTypes", versionFuncTypes);
        mmap.put("versionOperateTypes", versionOperateTypes);
        mmap.put("versioinItem", versioinItem);
        return prefix + "/versionitem_add";
    }

    @Log(title = "软件版本明细修改", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:version:edit")
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(SysVersionItem sysVersionItem)
    {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= sysVersionItemService.updateVersionItem(sysVersionItem);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    @RequiresPermissions("system:version:delete")
    @Log(title = "软件版本明细删除", businessType = BusinessType.DELETE)
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult deleteVersionItem(@RequestParam("id") String id) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = sysVersionItemService.deleteVersionItem(id);
            return ajaxResult;
        }
        catch (Exception e) {
            return AjaxResult.error(this.getExceptionMessage(e));
        }
    }
}
